News Commentaries of the U.S. Newsmakers

by @tozCSS


In [56]:
import pandas as pd
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')

In [5]:
cd ../


C:\Users\Talha\Documents\WinPython3\projects\News-Commentary-Tweets-of-Elites

In [23]:
df = pd.read_csv('data/US-headlines.csv',usecols=['newsdate','title','twcount'],parse_dates=[0])
df = df[~df.title.str.contains('The\s?Plazz')]
df13 = df[df['newsdate']<'2014-01-10']
df14 = df[df['newsdate']>='2014-01-10']
df.sort('twcount',ascending=False)


Out[23]:
newsdate twcount title
4922 2013-12-05 307 Apartheid resistance leader Mandela has died
4986 2013-11-28 270 A day full of thanks, feasts and families
7178 2013-02-24 227 Academy Awards 2013: cheers and jeers
4369 2014-01-28 199 Obama promises a year of action for 2014
4714 2013-12-24 196 Season’s tweetings: Tis time to gather ’round
4302 2014-02-02 192 Actor Philip Seymour Hoffman dead at 46
122 2014-12-31 181 Celeb greetings wish the happiest of new years
4660 2013-12-31 175 Billions ring in the New Year around the world
6439 2013-06-19 174 James Gandolfini dies of heart attack in Italy
2685 2014-06-10 174 Tea Party’s Brat hands Cantor primary defeat
562 2014-11-27 172 Celebs share gratitude on Thanksgiving
6863 2013-04-08 169 Britain’s Iron Lady dies of stroke at 87
192 2014-12-25 165 Season’s tweetings: It’s Christmas Day greetings
4388 2014-01-26 162 Macklemore, Lorde, Daft Punk sweep Grammys
2844 2014-05-28 162 Renowned poet, author Maya Angelou dies
4567 2014-01-09 153 NJ Gov. Christie claims ignorance, apologizes
7250 2013-02-14 151 Love and celebrity hawking on Valentine’s
7114 2013-03-06 150 Rand Paul leads epic filibuster on CIA nominee
5211 2013-10-31 149 Trick or tweet! Halloween is here
4704 2013-12-25 146 Celebs spread holiday cheer to one and all
7410 2013-01-21 145 “Our journey is not complete”
6301 2013-07-04 142 Celebs show their patriotic colors
5258 2013-10-27 140 Legendary rock pioneer dead at 71
4529 2014-01-13 139 Hollywood’s hottest stars snag Globes
3085 2014-05-11 138 Americans mark 100 years of Mother’s Day
5119 2013-11-11 135 Remembering those who offered their lives
5807 2013-08-31 131 Obama delays Syria strike for Congress vote
1540 2014-09-11 131 US observes somber 9/11 anniversary
7334 2013-02-01 131 Old scores settled at Hagel hearing
7067 2013-03-13 129 A New World ‘first': Pope Francis
... ... ... ...
3091 2014-05-11 3 Facing elimination, Rangers force Game 7
4056 2014-02-20 3 Bay Area residents keep drawing lucky numbers
4062 2014-02-20 3 GOProud co-founder quits over CPAC invite
3190 2014-05-02 3 UK celeb publicist given jail for sexual assaults
1461 2014-09-18 3 AOL shares go vertical amid Yahoo merger talk
7301 2013-02-06 3 Ravens parade through Baltimore
1522 2014-09-12 3 School shooter TJ Lane escapes Ohio prison
762 2014-11-10 3 Students ask Rick Perry about anal sex in protest
4487 2014-01-17 3 Two students shot at Philadelphia school
1549 2014-09-11 3 Sens. Flake, Heinrich star in survival reality...
2110 2014-07-28 3 NPR journalist, Wiccan Margot Adler dies at 68
2501 2014-06-24 3 Who gets dumped in Italy on ‘Bachelorette?’
1798 2014-08-22 3 Bodies of MH17 victims return to Malaysia
3294 2014-04-25 3 Conn. teen killed after turning down prom date
2089 2014-07-29 3 OKcupid messes with users’ compatibility
4456 2014-01-20 3 Central African Republic welcomes female leader
3268 2014-04-28 3 Syria’s al-Assad formally files reelection bid
863 2014-11-03 3 NASCAR drivers in brawl at Texas speedway
5088 2013-11-16 3 Jay Z decides to work with Barney’s
1515 2014-09-13 3 Zimmerman faces no charges for death threat
6339 2013-06-30 3 Italy claims third at Confederations Cup
3265 2014-04-28 3 Kissing congressman McAllister won’t run
3962 2014-02-27 3 NFL mulls penalty for use of N-word on field
4426 2014-01-23 3 States drag feet on Medicaid expansion
897 2014-10-30 3 Cruz’s comment on gay CEO criticized, praised
3984 2014-02-26 3 SCOTUS ruling eases warrantless searches
919 2014-10-29 3 Poet and Pulitzer winner Galway Kinnell dies a...
4407 2014-01-24 3 One killed in S.C. University shooting
944 2014-10-27 3 ‘Citizenfour’ proves to be effective horror film
671 2014-11-18 3 ‘Dumb & Dumber To’ surprise Box Office win

7367 rows × 3 columns


In [65]:
# group by week
# kw_year = lambda x: str(x.year) + ' - ' + str(x.isocalendar()[1])
gw13 = df13.groupby(df['newsdate'].map(lambda x: x.isocalendar()[1])).sum()
gw14 = df14.groupby(df['newsdate'].map(lambda x: x.isocalendar()[1])).sum()
gm13 = df13.groupby(df['newsdate'].map(lambda x: x.month)).sum()
gm14 = df14.groupby(df['newsdate'].map(lambda x: x.month)).sum()

In [76]:
# is there a seasonal pattern that we see every year?
plt.plot(gw13);
plt.plot(gw14);
plt.legend(['2013','2014'],loc=4);
plt.title('Weekly Commentation of US Newsmakers');
plt.ylabel('# of tweets');
plt.xlabel('week of the year');



In [77]:
daily = df.groupby('newsdate').sum()
daily.plot()


Out[77]:
<matplotlib.axes._subplots.AxesSubplot at 0x89e0be0>

In [8]:
data = Data([Scatter(x=daily.index,y=daily.twcount)])
twc = df.groupby('newsdate').apply(lambda t: t[t.twcount == t.twcount.max()])
titles = twc[twc.newsdate.isin(daily.sort('twcount',ascending=False).head(11).index.values)]
titles = titles.sort('twcount',ascending=False)
titles = titles.reset_index(drop=True)
titles = pd.merge(titles,daily,left_on='newsdate',right_index=True)
titles.rename(columns={'twcount_x': 'twcount', 'twcount_y': 'twdaily'}, inplace=True)
titles


Out[8]:
newsdate twcount title twdaily
0 2013-12-05 307 Apartheid resistance leader Mandela has died 481
1 2014-01-28 199 Obama promises a year of action for 2014 514
2 2014-02-02 192 Actor Philip Seymour Hoffman dead at 46 767
3 2013-09-10 123 Obama: ‘exceptional’ US ‘doesn’t do pinpricks’ 478
4 2014-12-17 99 Sony axes Christmas day release of ‘The Interv... 553
5 2014-12-09 98 CIA ‘Torture Report’ called ‘gruesome’ 460
6 2014-03-02 96 And the Oscars for Best actors go to … 649
7 2014-08-25 91 And the Emmy Award goes to … 472
8 2014-11-04 83 GOP wrestles Senate majority from Dems 559
9 2013-10-01 76 Government is closed, so now what? 475
10 2014-11-25 58 Ferguson protests again marred by violence 473

In [14]:
def annotator(r):
    if (r['newsdate'] == pd.to_datetime('2013-10-01') or
        r['newsdate'] == pd.to_datetime('2014-12-09') or
        r['newsdate'] == pd.to_datetime('2014-12-17')):
        y = r['twdaily']-10 
    else:
        y = r['twdaily']+30
    return Annotation(x=r['newsdate'], y=y, xref='x', yref='y', text=r['title'], showarrow=False)

In [17]:
annotations = Annotations(map(annotator,titles.to_dict(orient='records')))
layout = Layout(title="News Commentary Tweet Counts of the U.S. Newsmakers (Jan 2013 - Jan 2015)",
                annotations=annotations,yaxis=YAxis(title='Daily tweet counts'))
fig = Figure(data=data, layout=layout)

In [18]:
py.iplot(fig,filename="News Commentary Tweet Counts of the U.S. Newsmakers")


Out[18]:

In [20]:
#these tweets belong to...
df = pd.read_csv('data/US-tweeps.csv',encoding='utf-8')
tweeps = df.groupby(by='twhandle')['twtext'].count().order(ascending=False).head(100)
tweeps.plot()


Out[20]:
<matplotlib.axes._subplots.AxesSubplot at 0x1196e0a50>

In [21]:
data = Data([Scatter(x=tweeps.index.values,y=tweeps.values)])
fig = Figure(data=data)
py.iplot(fig)


Out[21]: